﻿using AjaxPro;
using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Text;
using System.Web.UI;
using WorkFlow.DBProxy;
using WorkFlow.Engine;
using WorkFlow.Engine.NSActivity;

namespace WorkFlow.web
{
    [AjaxNamespace("ns")]
    public partial class Desktop : Page
    {
        /// <summary>
        ///     获取新
        /// </summary>
        /// <returns></returns>
        [AjaxMethod]
        public int actRequest()
        {
            int caseCount = 0;
            ReadOnlyCollection<WfActivity> list = WfActivityController.GetReadyActList();
            if (list == null || list.Count == 0) return 0;
            int uid = Convert.ToInt32(Session["USERID"]);
            foreach (WfActivity v in list)
            {
                if (v.PrivilegeRules != null && v.IsUserHasPrivilege(uid))
                {
                    caseCount++;
                }
            }
            return caseCount;
        }

        /// <summary>
        ///     构建菜单
        /// </summary>
        [AjaxMethod]
        public string ConstructMenus()
        {
            DataTable dt = GetModuls();
            if (dt == null) return "";

            var build = new StringBuilder();
            build.Append("<ul>");

            string li = @"<li><a href='{0}' target='right'>{1}</a></li>";
            foreach (DataRow v in dt.Rows)
            {
                build.Append(string.Format(li, v["F_URL"], v["F_NAME"]));
            }

            build.Append("</ul>");
            return build.ToString();
        }

        public DataTable GetModuls()
        {
            //获取该用户所拥有权限的模块命名空间及相对url
            string sql = string.Format(@"select F_ID, F_NAME,F_NS,F_URL  from T_MODULES where f_id in (
                                              select f_module_id from t_role_module where f_role_id in (
                                                 select ru.f_role_id from t_users u inner join t_role_user ru on u.f_id=ru.f_user_id
                                                        where u.f_id= {0}
                                              )
                                            )
                                            union
                                            select  F_ID, F_NAME,F_NS,F_URL   from T_MODULES where f_id in (
                                              select um.f_module_id from t_users u inner join t_user_module um on u.f_id=um.f_user_id and
                                              u.f_id={0}
                                            )", Session["USERID"]);

            return DB.MapToDataTable(sql);
        }

        [AjaxMethod]
        public string getUserID()
        {
            return "[" + Session["USERID"].ToString() + "]" +
                   DB.GetFirstField("select f_name from t_users where f_id =" + Session["USERID"]).ToString();
        }

        [AjaxMethod]
        public void ReLogin()
        {
            Logout();
        }

        public void Logout()
        {
            Session["USERID"] = null;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Utility.RegisterTypeForAjax(GetType(), Page);
            if (Session["USERID"] == null)
            {
                Response.Redirect("login.aspx");
            }
        }
    }
}